/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-09-26 18:57:25
 * @LastEditors: 商晓彬
 * @LastEditTime: 2022-01-04 14:37:21
 */
import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { MessageService } from '@abp/message/message.service';
import {
    CreateModifManageInput, MoadifManageServiceProxy, CommonServiceProxy,
    ApproveInput
} from '@shared/service-proxies/service-proxies';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { DetailComponent } from './detail/detail.component';
@Component({
    selector: 'app-procedure-change',
    templateUrl: './procedure-change.component.html',
    styleUrls: ['./procedure-change.component.less']
})
export class ProcedureChangeComponent extends AppComponentBase {
    @ViewChild('DetailComponent') DetailComponent: DetailComponent;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;
    //引入子组件
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Input() rowdata: any;  //行数据
    @Input() title: string;  //标题
    @Input() flag: boolean;  //0审批1查看
    editData: any;
    saving = false;
    active = false;
    form = new CreateModifManageInput();
    selectRow: any;
    sonTitle = undefined;
    sendTimeOptions = [];
    moduleOptions = [];
    changeTypeOption = [
        {
            label: '请选择',
            value: undefined
        }
    ];
    reasonOption = [
        {
            label: '请选择',
            value: undefined
        }
    ];
    approvalList = [];
    changeId = undefined;
    maintTitle = undefined;
    approvalForm = new ApproveInput();
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _moadifManageServiceProxy: MoadifManageServiceProxy,
        private _commonServiceProxy: CommonServiceProxy,
    ) {
        super(injector);
    }
    getClass(data) {
        if (data.result == 0) {
            return 'approval-info';
        } else if (data.result == 1) {
            return 'approval-success';
        } else if (data.result == 2) {
            return 'approval-error';
        }

    }
    ngOnInit() {
    }
    //打开弹窗
    onShown(): void {
        let guid = (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
        let guidCode = guid + guid + "-" + guid + "-" + guid + "-" + guid + "-" + guid + guid + guid;
        this.changeId = guidCode;
        this.active = true;
        this.changeTypeOption = [
            {
                label: '请选择',
                value: undefined
            }
        ];
        this._commonServiceProxy.getTypeList(
            "变更类型"
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.changeTypeOption = result;
            });
        this.reasonOption = [
            {
                label: '请选择',
                value: undefined
            }
        ];
        this._commonServiceProxy.getTypeList(
            "变更原因"
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.reasonOption = result;
            });
        //型号
        this._commonServiceProxy.getModelList(
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.moduleOptions = [];
                this.moduleOptions = result;
            });
    }
    onChange(event) {
        //发次
        this._commonServiceProxy.getLotNoList(
            this.form.modelId
        ).pipe(finalize(() => { }))
            .subscribe(result => {
                this.sendTimeOptions = [];
                this.sendTimeOptions = result;
            });
    }
    //查看新增任务
    show(): void {
        if (this.title == '审批') {
            this.initEditInfo();//初始化数据
        }
        this.active = true;
        this.modal.show();
    }
    initEditInfo() {
        abp.ui.setBusy();
        this._moadifManageServiceProxy.displayBusinessById(this.rowdata.bAppId)
            .pipe(finalize(() => { }))
            .subscribe((result) => {
                abp.ui.clearBusy();
                this.approvalList = result.workAuditorList;
                this.editData = result.modifTechnique;
                this.form.id = this.editData.id;
                this.form.destinationTechniqueId = this.editData.destinationTechniqueId;
                this.form.drawingNo = this.editData.drawingNo;
                this.form.content = this.editData.content;
                this.form.model = this.editData.model;
                this.form.modelId = this.editData.modelId;
                this._commonServiceProxy.getLotNoList(
                    this.form.modelId
                ).pipe(finalize(() => { }))
                    .subscribe(result => {
                        this.sendTimeOptions = [];
                        this.sendTimeOptions = result;
                        this.form.lotNo = this.editData.lotNo;
                        this.form.lotNoId = this.editData.lotNoId;
                    });
                this.form.modelName = this.editData.modelName;
                this.form.modifyNumber = this.editData.modifyNumber;
                this.form.modifyReason = this.editData.modifyReason;
                this.form.modifyType = this.editData.modifyType;
                this.form.productOpinions = this.editData.productOpinions;
                this.form.reEdition = this.editData.reEdition;
                this.form.remark = this.editData.remark;
                this.form.stageSign = this.editData.stageSign;
                this.form.techniqueId = this.editData.techniqueId;
                this.form.techniqueName = this.editData.techniqueName;
                this.form.techniqueNumber = this.editData.techniqueNumber;
                this.form.createFlag = 1;
                this.primengTableHelper.records = [{
                    id: this.form.techniqueId,
                    techniqueNumber: this.form.techniqueNumber,
                    techniqueName: this.form.techniqueName,
                    edition: this.form.reEdition,
                    moduleCode: this.form.moduleCode
                }]
                this.primengTableHelper.totalRecordsCount = 1;
                this.selectRow = {
                    id: this.form.techniqueId,
                    techniqueNumber: this.form.techniqueNumber,
                    techniqueName: this.form.techniqueName,
                    edition: this.form.reEdition,
                    moduleCode: this.form.moduleCode,
                    destinationTechniqueId: this.editData.destinationTechniqueId
                }
            });
    }
    //关闭事件
    close(): void {
        //清空form表单
        this.approvalForm = new ApproveInput();
        this.form = new CreateModifManageInput();
        this.selectRow = [];
        this.active = false;
        this.modal.hide();
    }
    // 获取表格
    getTableList(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this._moadifManageServiceProxy.getMoadifTechnique(
            this.form.model,
            this.form.lotNo,
            undefined,
            undefined,
            undefined,
            undefined,
            this.form.techniqueName,
            this.form.techniqueNumber,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event)
        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.primengTableHelper.hideLoadingIndicator();
        });
    }
    resert() {
        this.form.techniqueNumber = undefined;
        this.form.techniqueName = undefined;
    }
    toMaintain() {
        if (this.title == '审批') {
            this.DetailComponent.show();
            this.DetailComponent.getTreeInfo();
        }
    }
    accept() {
        abp.ui.setBusy();
        //审批同意1
        this.approvalForm.bappId = this.rowdata.bAppId;
        this.approvalForm.operation = 1;
        this._moadifManageServiceProxy.approveModif(this.approvalForm)
            .pipe(finalize(() => { }))
            .subscribe(() => {
                abp.ui.clearBusy();
                this._messageService.success('操作成功');
                this.modalSave.emit();
                this.close();
            });
    }
    confues() {
        abp.ui.setBusy();
        //审批拒绝0
        this.approvalForm.bappId = this.rowdata.bAppId;
        this.approvalForm.operation = 0;
        this._moadifManageServiceProxy.approveModif(this.approvalForm)
            .pipe(finalize(() => { }))
            .subscribe(() => {
                abp.ui.clearBusy();
                this._messageService.success('操作成功');
                this.modalSave.emit();
                this.close();
            });
    }
}
